package org.itboys.core.controller.web;

import com.alibaba.fastjson.JSONObject;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.dto.goods.SundayGoodsDto;
import org.itboys.core.dto.goods.SundayGoodsSizeDto;
import org.itboys.core.entity.mongo.SundayGoods;
import org.itboys.core.entity.mongo.SundayGoodsSize;
import org.itboys.core.service.mongo.web.SundayGoodsService;
import org.itboys.core.service.mongo.web.SundayGoodsSizeService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2018/10/15
 * 描述：场景商品
 */
@Controller
@RequestMapping("/sunday/web/scene/goods")
public class SundayGoodsController extends BaseController {
    @Autowired
    private SundayGoodsService goodService;
    @Autowired
    private SundayGoodsSizeService goodsSizeService;

    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws IOException {
        PageResult<SundayGoodsDto> result = goodService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    /**
     * 查询尺码
     *
     * @param request
     * @param response
     * @param goodsCode
     * @throws IOException
     */
    @RequestMapping("/size/select")
    public void sizeSelect(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "goodsCode", required = true) String goodsCode
    ) throws IOException {
        List<SundayGoodsSizeDto> result = goodsSizeService.select(goodsCode);
        ResponseMessage.success(result);
    }


    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/selectAll")
    public void selectAll(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws IOException {
        PageResult<SundayGoodsDto> result = goodService.selectAll(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    /**
     * 新增或修改
     *
     * @param request
     * @param response
     * @param goods
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "sizeDetails", required = true) List<String> sizeDetails,
            @RequestParam(value = "newSizeDetails", required = true) List<String> newSizeDetails,
            @RequestParam(value = "costPrices", required = true) List<Double> costPrices,
            @RequestParam(value = "discountPrices", required = true) List<Double> discountPrices,
            @RequestParam(value = "goodsCodes", required = true) List<String> goodsCodes,
            @RequestParam(value = "codeIds", required = true) List<Long> codeIds,
            @RequestParam(value = "goodsIds", required = true) List<Long> goodsIds,
            @ModelAttribute SundayGoods goods
    ) {
        goodService.save(goods, sizeDetails,newSizeDetails, costPrices, discountPrices,
                goodsCodes, codeIds, goodsIds);
        ResponseMessage.success();
    }

    /**
     * 商品选择页面
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value = "sceneId", required = true) Long sceneId,
            @RequestParam(value = "xAxis", required = true) Integer xAxis,
            @RequestParam(value = "yAxis", required = true) Integer yAxis,
            @RequestParam(value = "goodsId", required = false) Long goodsId,
            Model model) {
        if (LongUtil.isNotZreo(goodsId)) {
            SundayGoodsDto goods = goodService.findOne(goodsId);
            model.addAttribute("goods", goods);
        }
        model.addAttribute("sceneId", sceneId);
        model.addAttribute("xAxis", xAxis);
        model.addAttribute("yAxis", yAxis);
        return "/sunday/scene/goods/input";
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value = "id", required = true) Long id
    ) {
        goodService.delete(id);
        ResponseMessage.success();
    }


    /**
     * 产品选择器
     *
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose")
    public String choose(HttpServletRequest request,
                         @RequestParam(value = "isSingle", required = true) Integer isSingle,//是否单选
                         @RequestParam(value = "callBack", required = false) String callBack,//回调参数
                         @RequestParam(value = "param", required = false) String param,//查询参数
                         @RequestParam(value = "chooseId", required = false) String chooseId,//选择器id
                         Model model) {
        model.addAttribute("param", param);
        model.addAttribute("callBack", callBack);
        model.addAttribute("chooseId", chooseId);
        if (isSingle == null) {
            return "/sunday/scene/goods/choose_erro";
        } else if (isSingle == 1) {
            return "/sunday/scene/goods/choose_single";
        }
        return "/sunday/scene/goods/choose_erro";
    }

}
